<?php
abstract class View {
    private static $view = null;
    private static $tplVars = array();
    private static $template = '';
    private static $viewDriver = null;
    public static $tplDir = '';
    public static $cacheDir = '';
    
    /**Enter description here...
     * @return View
     */
    private static  function getView() {
        if (null == self::$view) {
            $skin = self::getSkin();
            self::$tplDir =  TPL_DIR . '/' . $skin . '/' . MODULE_NAME . '/';
            self::$cacheDir = TMP_DIR . '/' . $skin . '/' . MODULE_NAME . '/';
            if (false == is_dir(self::$tplDir)) {
                @mkdir(self::$tplDir, 0777, true);
            }
            if (false == is_dir(self::$cacheDir)) {
                @mkdir(self::$cacheDir, 0777, true);
            }
            $viewType = Config::get('VIEW_ENGINE');
            switch ($viewType) {
                case VIEW_TYPE_SMARTY:
                    $driverName = 'SmartyView';
                break;
                case VIEW_TYPE_DISCUZ:
                    $driverName = 'DiscuzView';
                break;
                case VIEW_TYPE_PHP:
                    $driverName = 'PhpView';
                break;
                default:
                    throw new Exception('自定义的模板类请对代码进行相应修改');
                break;
            }
            require_once(PUMP_CORE . '/ViewDriver/' . $driverName . '.class.php' );
            self::$view = new $driverName();
        }
        return self::$view;
    }

    /**
     * 构造函数，类为单态类，禁止外部实例化
     */
    private function __construct() {}
    
    /**
     * @param mixed $var
     * @param mixed $value
     */
    public static function assign($var, $value="") {
        if (is_array($var)) {
            foreach($var as $key=>$val) self::assign($key, $val);
        } else if (is_object($var)) {
            $objectName = get_class($var);
            self::assign($objectName, (array)$var);
        } else {
            self::$tplVars[$var] = $value;
        }
    }
    
    public static function clearAllAssign() {
        self::$tplVars = array();
    }
    
    public static function setTemplate($template, $tplDir=MODULE_NAME) {
        self::$template  = $tplDir . '/' . $template . '.' . Config::get('TEMPLATE_SUFFIX');
    }
    
    private static function getSkin() {
        static $skin;
        if ($skin) return $skin;
        $varSkinName = Config::get('VAR_SKINNAME');
        $skin = $_POST[$varSkinName] ? $_POST[$varSkinName] : $_GET[$varSkinName];
        if ('' == $skin)
            $skin = $_COOKIE[$varSkinName];
        if ('' == $skin)
            $skin = Config::get('DEFAULT_SKIN');
        setcookie($varSkinName, $skin, strtotime("+1 month"), '/');
        return $skin;
    }
    
    public static function getTemplate() {
        if ('' == self::$template) 
            self::$template = MODULE_NAME . '/' . ucfirst (ACTION_NAME) . '.' . Config::get('TEMPLATE_SUFFIX');
        $skin = self::getSkin();
        $tplFile = TPL_DIR . '/' . $skin . '/' . self::$template;
        $tplDir = dirname($tplFile);
        if (false == is_dir($tplDir)) {
            @mkdir($tplDir, 0777, true);
        }
        return $tplFile;
    }

    public static function fetch($template = '', $tplDir=MODULE_NAME) {
        if ('' != $template) {
            self::setTemplate($template, $tplDir);
        }
        $template = self::getTemplate();
        if (false == file_exists($template)) {
            throw new Exception('模板"' . str_replace(TPL_DIR . '/', '', $template) . '"不存在');
        }
        $view = self::getView();
        $view->setVars(self::$tplVars);
        return $view->parseTpl($template);
    }

    abstract protected function setVars($vars);
    abstract protected function parseTpl($template);
}
?>